package com.quxian.chambercommerce.base;

import java.io.*;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-11-8
 * Time: 下午10:29
 * To change this template use File | Settings | File Templates.
 */
public class FileUploadAction extends ActionSupport {

    private static final long serialVersionUID = 572146812454l;
    private static final int BUFFER_SIZE = 2*1024;
    private File upload;         //网页中file字段的name
    private String uploadContentType;  //struts2中必须字段(文件类型字段),主要是set方法
    private String uploadFileName;     // struts2中必须字段(文件名称字段),主要是set方法
    private String imageFileName;   //要上传的图片在服务器中的名称
    private String imagePath;       //保存服务器中的路径
    private String pagePath;        //页面中要引用的url

    public String getPagePath() {
        return pagePath;
    }

    public void setPagePath(String pagePath) {
        this.pagePath = pagePath;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getImageFileName() {
        return imageFileName;
    }

    @Override

    public String execute() {
        try {
        imageFileName = new Date().getTime()+"";   //此名称用当前时间与上传图片名组成
        imagePath = ServletActionContext.getServletContext().getRealPath("thirdlib/ckeditor/images") + File.separator + imageFileName;
        System.out.println(imagePath);
        pagePath = getWebPath() + "thirdlib/ckeditor/images" + "/" + imageFileName; //页面引用位置
//        pagePath = URLUtils.URLEncoding(pagePath);
        System.out.println(pagePath);
        File imageFile = new File(imagePath);
        copy(upload, imageFile);

        HttpServletResponse response = ServletActionContext.getResponse();

            PrintWriter out = response.getWriter();
//            pagePath="http://img0.bdstatic.com/img/image/4601e30e924b899a9013ecdc7191f950a7b0208f521.jpg";

        // 将上传的图片的url返回给ckeditor
            out.println("<script language='javascript'>");
            out.println("parent.document.getElementById('cke_103_textInput').value = '" + pagePath + "'");
            out.println("parent.document.getElementById('cke_99_previewImage').src = '" + pagePath + "'");
            out.println("parent.document.getElementById('cke_99_previewImage').style.display = ''");

            out.println("</script>");

            out.flush();
            out.close();
            return null;
        } catch (IOException e) {
            return null;
        }
    }

    private String getWebPath() {
        HttpServletRequest request = ServletActionContext.getRequest();
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
        return basePath;
    }

    private static void copy(File src, File dst) {
        try {
            InputStream in = null;
            OutputStream out = null;
            try {
                in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
                out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
                byte[] buffer = new byte[BUFFER_SIZE];
                while (in.read(buffer) > 0) {
                    out.write(buffer);
                }
            } finally {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            }
        } catch (Exception e) {
        }
    }
}
